package com.gx.smart.building.ui.common.util;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;

import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.logger.Logger;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author xiaosy
 * @create 2020/9/3
 * @Describe
 **/
public class ImageSaveUtil {


    public static Uri saveBitmap(Bitmap bitmap, Context context) {
        String displayName = System.currentTimeMillis() + ".jpg";
        String mimeType = "image/jpeg";
        Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
        return addBitmapToAlbum(bitmap, displayName, mimeType, compressFormat, context.getContentResolver(), true);
    }

    private static Uri addBitmapToAlbum(Bitmap bitmap, String displayName, String mimeType, Bitmap.CompressFormat compressFormat, ContentResolver contentResolver, boolean isTip) {
        Logger.d("addBitmapToAlbum");
        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName);
        values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM);
        } else {
            values.put(MediaStore.MediaColumns.DATA, Environment.getExternalStorageDirectory().getPath() + "/" + Environment.DIRECTORY_DCIM + "/" + displayName);
        }
        Uri uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        if (uri != null) {
            try {
                OutputStream outputStream = contentResolver.openOutputStream(uri);
                if (outputStream != null) {
                    bitmap.compress(compressFormat, 100, outputStream);
                    outputStream.flush();
                    outputStream.close();
                    if (isTip) {
                        ToastUtils.showLong("保存图片成功");
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Logger.e(e, "addBitmapToAlbum");
            } catch (IOException e) {
                e.printStackTrace();
                Logger.e(e, "addBitmapToAlbum");
            }

        }
        return uri;
    }

    /**
     * 根据uri地址获取文件path
     *
     * @param context
     * @param uri
     * @return
     */
    @SuppressLint("Range")
    public static String getFilePathByUri(Context context, Uri uri) {
        String path = null;
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        if (cursor == null) {
            return null;
        }
        if (cursor.moveToFirst()) {
            try {
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        cursor.close();
        return path;
    }

    /**
     * 分享图片获取file path
     *
     * @param bitmap
     * @param context
     * @return
     */
    public static String shareBitmap(Bitmap bitmap, Context context) {
        String displayName = System.currentTimeMillis() + ".jpg";
        String mimeType = "image/jpeg";
        Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
        Uri uri = addBitmapToAlbum(bitmap, displayName, mimeType, compressFormat, context.getContentResolver(), true);
        if (uri != null) {
            return getFilePathByUri(context, uri);
        }
        return "";
    }


    public static void downloadBitmap(Bitmap bitmap, Context context) {
        String displayName = System.currentTimeMillis() + ".jpg";
        String mimeType = "image/jpeg";
        Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
        addBitmapToAlbum(bitmap, displayName, mimeType, compressFormat, context.getContentResolver(), false);
    }
}
